package com.tool.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.MatteBorder;

import com.tool.DBConnector;
import com.tool.DataCenterPersistence;
import com.tool.DataCenterVO;
import com.tool.ResourceBundleHelper;

/**
 * 数据中心注册界面
 * 
 * @author Evan.Hu
 * 
 */
public class DCRegisterUI extends CoreUI {

	private static final long serialVersionUID = 2129621214582988405L;

	JLabel dbTypeLabel;
	JComboBox dbTypeComboBox = null;
	JLabel dbAddrLabel;
	JTextField dbAddrTxt;
	JLabel dbPortLabel;
	JTextField dbPortTxt;
	JLabel dbUserLabel;
	JTextField dbUserTxt;
	JLabel dbPwdLabel;
	JPasswordField dbPwdTxt;
	JLabel dbInstanceLabel;
	JTextField dbInstanceTxt;
	JButton testDBBtn;
	JLabel dcNumberLabel;
	JTextField dcNumberTxt;
	JLabel dcNameLabel;
	JTextField dcNameTxt;

	JButton confirmBtn;
	JButton cancelBtn;

	public DCRegisterUI() {
		super();
		setPreferredSize(new Dimension(660, 250));
		initUI();
	}

	private void initUI() {
		resHelper = new ResourceBundleHelper(DCRegisterUI.class.getName());

		setTitle(resHelper.getString("Frame.title"));
		final MatteBorder bottomLineBorder = BorderFactory.createMatteBorder(0, 0, 1, 0, Color.GRAY);
		setLayout(null);

		// R1C1: db Type
		dbTypeLabel = new JLabel(resHelper.getString("dbTypeLabel"));
		dbTypeLabel.setBounds(new Rectangle(20, 20, 80, 20));
		dbTypeLabel.setBorder(bottomLineBorder);
		add(dbTypeLabel, null);

		dbTypeComboBox = new JComboBox();
		dbTypeComboBox.setBounds(new Rectangle(100, 20, 110, 20));
		add(dbTypeComboBox, null);

		// R1C2: dbAddr
		dbAddrLabel = new JLabel(resHelper.getString("dbAddrLabel"));
		dbAddrLabel.setBounds(new Rectangle(210, 20, 80, 20));
		dbAddrLabel.setBorder(bottomLineBorder);
		add(dbAddrLabel, null);

		dbAddrTxt = new JTextField();
		dbAddrTxt.setBounds(new Rectangle(295, 20, 100, 20));
		add(dbAddrTxt, null);

		// R1C3: dbPort
		dbPortLabel = new JLabel(resHelper.getString("dbPortLabel"));
		dbPortLabel.setBounds(new Rectangle(400, 20, 80, 20));
		dbPortLabel.setBorder(bottomLineBorder);
		add(dbPortLabel, null);

		dbPortTxt = new JTextField();
		dbPortTxt.setBounds(new Rectangle(485, 20, 100, 20));
		add(dbPortTxt, null);

		// R2C1: Db User
		dbUserLabel = new JLabel(resHelper.getString("dbUserLabel"));
		dbUserLabel.setBounds(new Rectangle(20, 50, 80, 20));
		dbUserLabel.setBorder(bottomLineBorder);
		add(dbUserLabel, null);

		dbUserTxt = new JTextField();
		dbUserTxt.setBounds(new Rectangle(105, 50, 100, 20));
		add(dbUserTxt, null);

		// R2C2: DB Password
		dbPwdLabel = new JLabel(resHelper.getString("dbPwdLabel"));
		dbPwdLabel.setBounds(new Rectangle(210, 50, 80, 20));
		dbPwdLabel.setBorder(bottomLineBorder);
		add(dbPwdLabel, null);

		dbPwdTxt = new JPasswordField();
		dbPwdTxt.setBounds(new Rectangle(295, 50, 100, 20));
		add(dbPwdTxt, null);

		// R2C3: DB Instance
		dbInstanceLabel = new JLabel(resHelper.getString("dbInstanceLabel"));
		dbInstanceLabel.setBounds(new Rectangle(400, 50, 80, 20));
		dbInstanceLabel.setBorder(bottomLineBorder);
		add(dbInstanceLabel, null);

		dbInstanceTxt = new JTextField();
		dbInstanceTxt.setBounds(new Rectangle(485, 50, 100, 20));
		add(dbInstanceTxt, null);

		// R3C1: DC Number
		dcNumberLabel = new JLabel(resHelper.getString("dcNumberLabel"));
		dcNumberLabel.setBounds(new Rectangle(20, 79, 80, 20));
		dcNumberLabel.setBorder(bottomLineBorder);
		add(dcNumberLabel, null);

		dcNumberTxt = new JTextField();
		dcNumberTxt.setBounds(new Rectangle(105, 79, 100, 20));
		add(dcNumberTxt, null);

		// R3C2: DC Name
		dcNameLabel = new JLabel(resHelper.getString("dcNameLabel"));
		dcNameLabel.setBounds(new Rectangle(210, 79, 80, 20));
		dcNameLabel.setBorder(bottomLineBorder);
		add(dcNameLabel, null);

		dcNameTxt = new JTextField();
		dcNameTxt.setBounds(new Rectangle(295, 79, 100, 20));
		add(dcNameTxt, null);

		// R3C3: testDB button
		testDBBtn = new JButton();
		testDBBtn.setAction(new AbstractAction(resHelper.getString("testDBBtn")) {
			public void actionPerformed(ActionEvent e) {
				try {
					testDB_ActionPerformed(e);
				} catch (Exception e1) {
					handUIException(e1);
				}
			}
		});
		testDBBtn.setBounds(new Rectangle(400, 79, 80, 20));
		add(testDBBtn, null);

		// R4C1: confirm Button
		confirmBtn = new JButton();
		confirmBtn.setBounds(new Rectangle(20, 119, 80, 20));
		confirmBtn.setAction(new AbstractAction(resHelper.getString("confirmBtn")) {
			public void actionPerformed(ActionEvent e) {
				try {
					confirm_ActionPerformed(e);
				} catch (Exception e1) {
					handUIException(e1);
				}
			}
		});
		add(confirmBtn, null);

		// R4C2: cancel Button
		cancelBtn = new JButton();
		cancelBtn.setBounds(new Rectangle(105, 119, 80, 20));
		cancelBtn.setAction(new AbstractAction(resHelper.getString("cancelBtn")) {
			public void actionPerformed(ActionEvent e) {
				try {
					cancel_ActionPerformed(e);
				} catch (Exception e1) {
					handUIException(e1);
				}
			}
		});
		add(cancelBtn, null);

	}

	public void onLoad() {

		setResizable(false);

		// DB Type
		dbTypeComboBox.addItem("MS SqlServer");
		dbTypeComboBox.addItem("Oracle");

	}

	/**
	 * 刷新连接数据库信息
	 * 
	 * @param e
	 */
	public void testDB_ActionPerformed(ActionEvent e) throws Exception {
		DBConnector.testDB(getDCVO());
		JOptionPane.showMessageDialog(getWindow(), "连接成功");
	}

	/**
	 * 注册
	 * 
	 * @param e
	 */
	public void confirm_ActionPerformed(ActionEvent e) throws Exception {
		final DataCenterVO dcVo = getDCVO();
		DBConnector.testDB(dcVo);
		DataCenterPersistence.register(dcVo);
		JOptionPane.showMessageDialog(getWindow(), "注册成功");

	}

	/**
	 * 关闭
	 * 
	 * @param e
	 */
	public void cancel_ActionPerformed(ActionEvent e) throws Exception {
		getWindow().dispose();
	}

	private DataCenterVO getDCVO() {
		DataCenterVO dc = new DataCenterVO();
		dc.setDbType(dbTypeComboBox.getSelectedIndex());
		dc.setDbAddr(dbAddrTxt.getText());
		dc.setDbPort(dbPortTxt.getText());
		dc.setUserName(dbUserTxt.getText());
		dc.setPassword(dbPwdTxt.getText());
		dc.setDbName(dbInstanceTxt.getText());
		dc.setDcNumber(dcNumberTxt.getText());
		dc.setDcName(dcNameTxt.getText());

		// check
		if (isStringEmpty(dc.getDbAddr())) {
			throw new RuntimeException("[数据库地址]不能为空");
		}
		if (isStringEmpty(dc.getDbPort())) {
			throw new RuntimeException("[数据库端口]不能为空");
		}
		if (isStringEmpty(dc.getUserName())) {
			throw new RuntimeException("[数据库用户]不能为空");
		}
		if (isStringEmpty(dc.getPassword())) {
			throw new RuntimeException("[数据库密码]不能为空");
		}
		if (isStringEmpty(dc.getDbName())) {
			throw new RuntimeException("[数据库名]不能为空");
		}
		if (isStringEmpty(dc.getDcNumber())) {
			throw new RuntimeException("[数据中心代码]不能为空");
		}
		if (isStringEmpty(dc.getDcName())) {
			throw new RuntimeException("[数据中心名称]不能为空");
		}

		return dc;
	}

	public static void main(String[] args) {

		new DCRegisterUI().showUI();
	}
}
